<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.14.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="C++实现采用 right、down指针的方法，维护前驱与后继、上层与下层的节点之间的关系。 缺点：每个节点都要存储真实的数据，增加了不必要的开销。 (另外，我的代码实现有点冗余，其实可以把查找节点抽象一个方法出来。 ) 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849">
<meta property="og:type" content="article">
<meta property="og:title" content="SkipList">
<meta property="og:url" content="http://example.com/2021/12/16/SkipList/index.html">
<meta property="og:site_name" content="JsyBlog">
<meta property="og:description" content="C++实现采用 right、down指针的方法，维护前驱与后继、上层与下层的节点之间的关系。 缺点：每个节点都要存储真实的数据，增加了不必要的开销。 (另外，我的代码实现有点冗余，其实可以把查找节点抽象一个方法出来。 ) 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-12-16T06:23:04.000Z">
<meta property="article:modified_time" content="2021-12-16T06:26:25.885Z">
<meta property="article:author" content="SongyangJi">
<meta property="article:tag" content="数据结构">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2021/12/16/SkipList/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2021/12/16/SkipList/","path":"2021/12/16/SkipList/","title":"SkipList"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>SkipList | JsyBlog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">JsyBlog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#C-%E5%AE%9E%E7%8E%B0"><span class="nav-number">1.</span> <span class="nav-text">C++实现</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8A%9B%E6%89%A3%E3%80%8A%E8%AE%BE%E8%AE%A1%E8%B7%B3%E8%A1%A8%E3%80%8B"><span class="nav-number">2.</span> <span class="nav-text">力扣《设计跳表》</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">SongyangJi</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">251</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">45</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">109</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/12/16/SkipList/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="SkipList | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          SkipList
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-12-16 14:23:04 / 修改时间：14:26:25" itemprop="dateCreated datePublished" datetime="2021-12-16T14:23:04+08:00">2021-12-16</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="url" rel="index"><span itemprop="name">数据结构</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="C-实现"><a href="#C-实现" class="headerlink" title="C++实现"></a>C++实现</h1><p>采用 <code>right</code>、<code>down</code>指针的方法，维护前驱与后继、上层与下层的节点之间的关系。</p>
<p><strong>缺点</strong>：每个节点都要存储真实的数据，增加了不必要的开销。</p>
<p>(<del>另外，我的代码实现有点冗余，其实可以把查找节点抽象一个方法出来。</del> )</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> std::vector;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">SkipList</span> &#123;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="comment">// 最高层数为32层</span></span><br><span class="line">    <span class="type">static</span> <span class="type">const</span> <span class="type">int</span> MAX_LEVELS = <span class="number">32</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">struct</span> <span class="title class_">Node</span> &#123;</span><br><span class="line">        <span class="comment">// 真实数据</span></span><br><span class="line">        <span class="type">int</span> val;</span><br><span class="line">        <span class="comment">// 向右走、向下走的指针</span></span><br><span class="line">        Node *right, *down;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">explicit</span> <span class="title">Node</span><span class="params">(<span class="type">int</span> val = <span class="number">0</span>, Node *right = <span class="literal">nullptr</span>, Node *down = <span class="literal">nullptr</span>)</span> : val(val), right(right), down(down) &#123;</span>&#125;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 哑结点</span></span><br><span class="line">    Node *head = <span class="literal">nullptr</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 模拟抛硬币</span></span><br><span class="line">    <span class="function"><span class="type">static</span> <span class="type">bool</span> <span class="title">getRand</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="type">int</span> x = <span class="built_in">rand</span>() % <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">return</span> x;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">SkipList</span>() &#123;</span><br><span class="line">        <span class="comment">// 哑结点初始化MAX层，next指针都为空，head是最上面的节点</span></span><br><span class="line">        <span class="comment">// 哑结点不存储任何真实数据，仅仅是为了代码实现方便罢了</span></span><br><span class="line">        Node *down = <span class="literal">nullptr</span>, *up;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; MAX_LEVELS; i++) &#123;</span><br><span class="line">            up = <span class="keyword">new</span> <span class="built_in">Node</span>(<span class="number">0</span>, <span class="literal">nullptr</span>, down);</span><br><span class="line">            down = up;</span><br><span class="line">        &#125;</span><br><span class="line">        head = up;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 查找某个元素是否存在</span></span><br><span class="line">    <span class="function"><span class="type">bool</span> <span class="title">search</span><span class="params">(<span class="type">int</span> target)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 从左上角出发</span></span><br><span class="line">        Node *node = head;</span><br><span class="line">        <span class="keyword">while</span> (node) &#123;</span><br><span class="line">            <span class="comment">//先向右移，再向下移</span></span><br><span class="line">            <span class="keyword">if</span> (node-&gt;right) &#123;</span><br><span class="line">                <span class="comment">// 此时  node.val &lt; target &lt; node.val, 通过“跳跃”的方式快速缩小了范围</span></span><br><span class="line">                <span class="keyword">if</span> (target &lt; node-&gt;right-&gt;val) &#123;</span><br><span class="line">                    node = node-&gt;down;</span><br><span class="line">                    <span class="comment">// 找到了   </span></span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (node-&gt;right-&gt;val == target) &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">                    <span class="comment">// 继续往右走    </span></span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    node = node-&gt;right;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// 右面没有节点了，只能往下走才可能找到    </span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                node = node-&gt;down;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 这里的新增节点，是允许重复值的；如果是Set的话，可以先查找出目标 node ，如果 node.val ==  num , 就不要重复插入了</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">add</span><span class="params">(<span class="type">int</span> num)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 其实是一个栈，用来存储搜索路径中的节点(前驱)，</span></span><br><span class="line">        <span class="comment">// 因为最后要根据新节点的层数往上层增加节点，就必须知道它的前驱</span></span><br><span class="line">        vector&lt;Node *&gt; downs;</span><br><span class="line">        <span class="comment">// 从顶部开始搜索。</span></span><br><span class="line">        Node *node = head;</span><br><span class="line">        <span class="keyword">while</span> (node) &#123;</span><br><span class="line">            <span class="comment">// 一直往右走，直到右边没有节点，或者右边节点的值大于 num</span></span><br><span class="line">            <span class="keyword">while</span> (node-&gt;right &amp;&amp; node-&gt;right-&gt;val &lt;= num) &#123;</span><br><span class="line">                node = node-&gt;right;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//找到这一层的找到要插入的位置的前驱, 入栈</span></span><br><span class="line">            downs.<span class="built_in">push_back</span>(node);</span><br><span class="line">            <span class="comment">// 往下走，直到最底层</span></span><br><span class="line">            node = node-&gt;down;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 这时候已经到了最底层</span></span><br><span class="line">        <span class="comment">// 从后向前根据“抛硬币的节点”依次在每一层的前驱后面插入节点;</span></span><br><span class="line">        <span class="type">int</span> pos = downs.<span class="built_in">size</span>() - <span class="number">1</span>;</span><br><span class="line">        Node *dn = <span class="literal">nullptr</span>, *pre;</span><br><span class="line">        <span class="keyword">do</span> &#123;</span><br><span class="line">            pre = downs[pos--];</span><br><span class="line">            <span class="comment">// 新增节点存储值是 num （明显数据被重复存储了），后继是 pre-&gt;right ,前驱是 pre（其实就是链表的插入操作），注意</span></span><br><span class="line">            pre-&gt;right = <span class="keyword">new</span> <span class="built_in">Node</span>(num, pre-&gt;right, dn);</span><br><span class="line">            <span class="comment">// 这时的 pre-&gt;right 就是新增的节点。</span></span><br><span class="line">            dn = pre-&gt;right;</span><br><span class="line">            <span class="comment">// 如果没有超出允许的 MAX_LEVELS 并且根据抛硬币的结果需要继续往上加，则继续  </span></span><br><span class="line">        &#125; <span class="keyword">while</span> (pos &gt;= <span class="number">0</span> &amp;&amp; <span class="built_in">getRand</span>());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 删除一个值,基本上与查找流程一致</span></span><br><span class="line">    <span class="function"><span class="type">bool</span> <span class="title">erase</span><span class="params">(<span class="type">int</span> num)</span> </span>&#123;</span><br><span class="line">        Node *node = head;</span><br><span class="line">        <span class="comment">// 记录是否存在</span></span><br><span class="line">        <span class="type">bool</span> exist = <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">while</span> (node) &#123;</span><br><span class="line">            <span class="keyword">if</span> (node-&gt;right) &#123;</span><br><span class="line">                <span class="comment">// 找到了目标值</span></span><br><span class="line">                <span class="keyword">if</span> (node-&gt;right-&gt;val == num) &#123;</span><br><span class="line">                    <span class="comment">// del 即为要删除的节点</span></span><br><span class="line">                    Node *del = node-&gt;right;</span><br><span class="line">                    <span class="comment">// 链表的删除操作</span></span><br><span class="line">                    node-&gt;right = del-&gt;right;</span><br><span class="line">                    <span class="comment">// 注意我们是要把这个&quot;柱子&quot;从上向右全部删除的，所以还要往下走，继续走</span></span><br><span class="line">                    <span class="comment">// node 是要删除的节点的前驱</span></span><br><span class="line">                    node = node-&gt;down;</span><br><span class="line">                    exist = <span class="literal">true</span>;</span><br><span class="line">                    <span class="keyword">delete</span> del;</span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (node-&gt;right-&gt;val &lt; num) &#123;</span><br><span class="line">                    node = node-&gt;right;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    node = node-&gt;down;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                node = node-&gt;down;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> exist;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>


<h1 id="力扣《设计跳表》"><a href="#力扣《设计跳表》" class="headerlink" title="力扣《设计跳表》"></a>力扣《设计跳表》</h1><p>力扣上的一道题目，可以供你测试你的代码。<br><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/design-skiplist/">设计跳表</a></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Node</span>&#123;</span><br><span class="line">    <span class="type">int</span> val;</span><br><span class="line">    Node *right, *down;</span><br><span class="line">    <span class="built_in">Node</span>(Node* _right, Node* _down, <span class="type">int</span> _val):<span class="built_in">right</span>(_right),<span class="built_in">down</span>(_down),<span class="built_in">val</span>(_val)&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Skiplist</span> &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Node* head;</span><br><span class="line">    vector&lt;Node*&gt; inserted;</span><br><span class="line">    <span class="built_in">Skiplist</span>() &#123;</span><br><span class="line">        head = <span class="keyword">new</span> <span class="built_in">Node</span>(<span class="literal">NULL</span>, <span class="literal">NULL</span>, <span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="type">bool</span> <span class="title">search</span><span class="params">(<span class="type">int</span> target)</span> </span>&#123;</span><br><span class="line">        Node *p = head;</span><br><span class="line">        <span class="keyword">while</span>(p)&#123;</span><br><span class="line">            <span class="keyword">while</span>(p-&gt;right &amp;&amp; p-&gt;right-&gt;val &lt; target) p = p-&gt;right;</span><br><span class="line">            <span class="keyword">if</span>(p-&gt;right &amp;&amp; p-&gt;right-&gt;val == target) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">            p = p-&gt;down;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">add</span><span class="params">(<span class="type">int</span> num)</span> </span>&#123;</span><br><span class="line">        inserted.<span class="built_in">clear</span>();</span><br><span class="line">        Node *p = head;</span><br><span class="line">        <span class="keyword">while</span>(p)&#123;</span><br><span class="line">            <span class="keyword">while</span>(p-&gt;right &amp;&amp; p-&gt;right-&gt;val &lt; num) p = p-&gt;right;</span><br><span class="line">            inserted.<span class="built_in">push_back</span>(p);</span><br><span class="line">            p = p-&gt;down;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">bool</span> grow = <span class="literal">true</span>;</span><br><span class="line">        Node* downNode = <span class="literal">NULL</span>;</span><br><span class="line">        <span class="keyword">while</span>(grow &amp;&amp; !inserted.<span class="built_in">empty</span>())&#123;</span><br><span class="line">            Node* rightmost = inserted.<span class="built_in">back</span>(); inserted.<span class="built_in">pop_back</span>();</span><br><span class="line">            Node* newNode = <span class="keyword">new</span> <span class="built_in">Node</span>(rightmost-&gt;right, downNode, num);</span><br><span class="line">            rightmost-&gt;right = newNode;</span><br><span class="line">            downNode = newNode;</span><br><span class="line">            grow = (<span class="built_in">rand</span>()%<span class="number">2</span> == <span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 不人为限制层数，层数极高的情况很稀疏；</span></span><br><span class="line">        <span class="keyword">if</span>(grow)&#123;</span><br><span class="line">            Node* newNode = <span class="keyword">new</span> <span class="built_in">Node</span>(<span class="literal">NULL</span>, downNode, num);</span><br><span class="line">            head = <span class="keyword">new</span> <span class="built_in">Node</span>(newNode, head, <span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="type">bool</span> <span class="title">erase</span><span class="params">(<span class="type">int</span> num)</span> </span>&#123;</span><br><span class="line">        Node *p = head;</span><br><span class="line">        <span class="type">bool</span> seen = <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">while</span>(p)&#123;</span><br><span class="line">            <span class="keyword">while</span>(p-&gt;right &amp;&amp; p-&gt;right-&gt;val &lt; num) p = p-&gt;right;</span><br><span class="line">            <span class="keyword">if</span>(!seen &amp;&amp; p-&gt;right &amp;&amp; p-&gt;right-&gt;val == num) seen = <span class="literal">true</span>;</span><br><span class="line">            <span class="keyword">if</span>(seen)&#123;</span><br><span class="line">                p-&gt;right = p-&gt;right-&gt;right;</span><br><span class="line">            &#125;</span><br><span class="line">            p = p-&gt;down;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> seen;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Your Skiplist object will be instantiated and called as such:</span></span><br><span class="line"><span class="comment"> * Skiplist* obj = new Skiplist();</span></span><br><span class="line"><span class="comment"> * bool param_1 = obj-&gt;search(target);</span></span><br><span class="line"><span class="comment"> * obj-&gt;add(num);</span></span><br><span class="line"><span class="comment"> * bool param_3 = obj-&gt;erase(num);</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" rel="tag"># 数据结构</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/12/16/Redis-%E9%9B%86%E7%BE%A4/" rel="prev" title="Redis-集群">
                  <i class="fa fa-chevron-left"></i> Redis-集群
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/12/26/Java%E4%BD%BF%E7%94%A8jsch%E8%BF%9E%E6%8E%A5FTP%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6/" rel="next" title="Java使用jsch连接FTP服务器操作文件">
                  Java使用jsch连接FTP服务器操作文件 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">SongyangJi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  





</body>
</html>
